﻿using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Atlanchim.Crm.Mui.Pages.Emails
{
    public class PageEditEmailModel : AbstractPageEditModel
    {
        public Email Email { get; set; }

        public override void InnerLoad()
        {
            if (!string.IsNullOrEmpty(this.Fragment))
            {
                // Récupération des Ids
                int idEmail = 0;
                int.TryParse(this.DicFragments["IdEmail"], out idEmail);

                int idContact = 0;
                if (this.DicFragments.ContainsKey("IdContact"))
                {
                    int.TryParse(this.DicFragments["IdContact"], out idContact);
                }

                // Chargement ou création de l'entité
                if (idEmail > 0)
                {
                    this.Email = this._pageContext
                                        .Emails
                                        .Where(tel => tel.Id == idEmail)
                                        .FirstOrDefault();
                }
                else
                {
                    this.Email = new Email();
                    this.Email.IdContact = idContact;
                    this._pageContext.Emails.Add(this.Email);
                }
            }
        }
    }
}
